package com.itheima.reggie.web.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//点餐端购物车的表现层控制器
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    //添加购物车
    @PostMapping("/add")
    public ResultInfo addCart(@RequestBody Cart cartParam) {
        //调用service
        Cart cart = cartService.addCart(cartParam);
        //返回
        return ResultInfo.success(cart);
    }

    //查询购物车(用户只能查询自己的购物车)
    @GetMapping("/list")
    public ResultInfo findList() {
        //调用service
        List<Cart> cartList = cartService.findList();
        //返回
        return ResultInfo.success(cartList);
    }

    //删除购物车(用户只能删除自己的购物车)
    @DeleteMapping("/clean")
    public ResultInfo delete() {
        //调用service
        cartService.delete();
        //返回
        return ResultInfo.success();
    }

    //修改购物车
    @PostMapping("/sub")
    public ResultInfo update(@RequestBody Cart cartParam) {
        //调用service
        Cart cart = cartService.update(cartParam);
        if (cart == null) {
            cart = new Cart();
            cart.setNumber(0);
        }
        //返回
        return ResultInfo.success(cart);
    }
}
